/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.core.config;

import java.net.URI;

import io.gitee.huangjianbo.supos.openapi.core.errors.InvalidEndpointException;
import io.gitee.huangjianbo.supos.openapi.core.http.HttpScheme;
import io.gitee.huangjianbo.supos.openapi.core.tools.StringUtils;

/**
 * supOS访问地址
 *
 * @author tomcat
 * @version 1.0
 */
public class SuposEndpoint {

    /**
     * Defines the protocol scheme used for HTTP communication.
     * Defaults to {@link HttpScheme#HTTP}.
     */
    private HttpScheme scheme = HttpScheme.HTTP;
    /**
     * Represents the hostname of the endpoint for network communication.
     * This field is crucial for establishing connections and determining where requests should be directed.
     */
    private String host;
    /**
     * Specifies the network port number for the endpoint.
     * This integer value represents the communication endpoint for the service.
     * By default, it is set to 8080, which is a common default for web applications.
     */
    private Integer port = 8080;

    SuposEndpoint() {

    }

    SuposEndpoint(HttpScheme scheme, String host, Integer port) {
        this.scheme = scheme != null ? scheme : HttpScheme.HTTP;
        this.host = host;
        this.port = port != null ? port : 8080;
    }

    SuposEndpoint(String host, Integer port) {
        this.host = host;
        this.port = port != null ? port : 8080;
    }

    SuposEndpoint(String host) {
        this.host = host;
    }

    public static SuposEndpoint of() {
        return new SuposEndpoint();
    }

    public static SuposEndpoint of(HttpScheme scheme, String host, Integer port) {
        return new SuposEndpoint(scheme, host, port);
    }

    public static SuposEndpoint of(String host, Integer port) {
        return new SuposEndpoint(host, port);
    }

    public static SuposEndpoint of(String host) {
        return new SuposEndpoint(host);
    }

    public static SuposEndpoint of(URI uri) {
        return of(HttpScheme.valueOf(uri.getScheme().toUpperCase()), uri.getHost(), uri.getPort());
    }

    public SuposEndpoint scheme(HttpScheme scheme) {
        this.scheme = scheme != null ? scheme : HttpScheme.HTTP;
        return this;
    }

    public SuposEndpoint host(String host) {
        this.host = host;
        return this;
    }

    public SuposEndpoint port(Integer port) {
        this.port = port != null ? port : 8080;
        return this;
    }

    @Override
    public String toString() {
        return this.scheme.name().toLowerCase() + "://" + this.host + ":" + this.port;
    }

    public URI toURI() {
        validate();
        return URI.create(this.toString());
    }

    public HttpScheme getScheme() {
        return scheme;
    }

    public String getHttpScheme() {
        return scheme.name().toLowerCase();
    }

    public String getHost() {
        return host;
    }

    public Integer getPort() {
        return port;
    }

    public void validate() {
        if (this.scheme == null || StringUtils.isEmpty(this.host) || port == null) {
            throw new InvalidEndpointException("The field of [scheme, host, port] must be not null: " + this);
        }
    }
}
